//package sg.edu.nus.iss.billingsystem;
package sg.edu.nus.iss.billing.ui.controller;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import sg.edu.nus.iss.billing.common.exception.BadDaoCallException;
import sg.edu.nus.iss.billing.common.exception.BadDbCallException;
import sg.edu.nus.iss.billing.exception.customer.InvalidCustomerDataException;
import sg.edu.nus.iss.billing.exception.login.UserDoesNotExist;
import sg.edu.nus.iss.billing.service.account.AccountImpl;
import sg.edu.nus.iss.billing.service.customer.CustomerComplaintServiceImpl;
import sg.edu.nus.iss.billing.service.customer.CustomerServiceImpl;
import sg.edu.nus.iss.billing.service.login.LoginMemberServiceImpl;
import sg.edu.nus.iss.billing.service.login.UserImpl;
import sg.edu.nus.iss.billing.service.subscription.SubscriptionImpl;
import sg.edu.nus.iss.billing.ui.gui.MainForm;
import sg.edu.nus.iss.billing.vo.account.AccountVO;
import sg.edu.nus.iss.billing.vo.customer.CustomerComplaintVO;
import sg.edu.nus.iss.billing.vo.customer.CustomerVO;
import sg.edu.nus.iss.billing.service.subscription.SubscriptionImpl;
import sg.edu.nus.iss.billing.ui.gui.*;
import sg.edu.nus.iss.billing.vo.*;
import sg.edu.nus.iss.billing.vo.login.LoginVO;
import sg.edu.nus.iss.billing.vo.subscription.SubscriptionVO;


//import sg.edu.nus.iss.billingsystem.gui.MainForm;

public class OneBillingSystem {
	
	private MainForm mainform;
	private UserImpl user;
	
	public OneBillingSystem(){
		mainform = new MainForm(this);
	}
	
    public void start() {
    	LoginWindow loginwindow = new LoginWindow(this, mainform);
        loginwindow.show();
    	
        //mainform.setVisible (true);
    }
    
    public static void main (String args[]) {
    	OneBillingSystem manager = new OneBillingSystem ();
        manager.start ();
    }
    
   
    public void viewCustomerBill(String NRIC){
    	//Call user.ViewCustomerDetail
    	// should return BillVO
    }
    
    public void viewCustomerDetail(String NRIC){
    	//Call user.ViewCustomerDetail
    	//Should return CustomerVO
    }

    
    public void registerCustomer(){ //Parameter CustomerVO
    	//Call user.registerCustomer
    	//return CustomerVO;
    }
    
    public boolean deRegisterCustomer(String NRIC){ 
    	//Call user.deRegisterCustomer
    	return true;
    }
    
    public List<SubscriptionVO> getPlans(){
    	SubscriptionImpl subscriptionplanserviceimpl =new SubscriptionImpl();
    	return subscriptionplanserviceimpl.geAllPlans();
    }
    
    public boolean RegisterSubscription(String NRIC){ //2nd Parameter List of Subscriptions
    	//Call user.registerSubscription
    	return true;
    }
    
    public boolean DeRegisterSubscription(String NRIC){ //2nd Parameter List of Subscriptions
    	//Call user.deRegisterSubscription
    	return true;
    }
    
    public boolean generateBill(int month, int year){ 
    	//Call user.generateBill
    	return true;
    }
    
   
    public UserImpl getUser() {
        return this.user;
    }

public void setUser(UserImpl user) {
        this.user = user;
    }


  public MainForm getMainForm(){
        return mainform;
    }
  
  public UserImpl login(LoginVO loginvo){
  	LoginMemberServiceImpl loginMemberServiceImpl = new LoginMemberServiceImpl();
  	try {
			 user =loginMemberServiceImpl.findLoginMemberInfo(loginvo);
			 
		} catch (UserDoesNotExist e) {
  		JOptionPane.showMessageDialog(null,e.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
  			return null;
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null,e.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
			return null;
		}
  	return user;
  }
  
  
  //Customer Details--------------------------------------------------------------------------------------------------------
  public CustomerVO viewCustomerDetailByNRIC(CustomerVO customervo){
  	CustomerServiceImpl customerServiceImpl = new CustomerServiceImpl();
  	return customerServiceImpl.getCustomer(customervo);
  }
  
  public CustomerVO viewCustomerDetailByACCNo(AccountVO accountvo){
  	CustomerServiceImpl customerServiceImpl = new CustomerServiceImpl();
  	System.out.println("View Customer By Account No :: " + accountvo.getAccountNo());
  	return customerServiceImpl.getCustomerByAcc(accountvo);
  }
  public void deregisterCustomer(CustomerVO c) throws BadDbCallException{
	  AccountImpl acimpl = new AccountImpl();
	  acimpl.deregisterAccount(c.getNric(), c.getAccount(), c.getAccount().getStatus());
  }
  
  public String registerCustomer(CustomerVO customervo){ //Parameter CustomerVO
  	//Call user.registerCustomer
  	//boolean regStatus = false;
  	String result="";
  	CustomerServiceImpl customerServiceImpl = new CustomerServiceImpl();
  	//System.out.println("Customer Reg Start Point : [2]");
  	//regStatus=customerServiceImpl.registerCustomer(customervo);
  	//System.out.println("Customer Reg Start Point : [3] "+regStatus);
  	try {
  		result= customerServiceImpl.registerCustomer(customervo);
		} catch (InvalidCustomerDataException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
  	return result;
  }
  
  public boolean updateCustomerDetails(CustomerVO customervo){
		CustomerServiceImpl customerServiceImpl = new CustomerServiceImpl();
		return customerServiceImpl.updateCustomerDetail(customervo);
	  }
  
  public boolean createCustomerComplaint(CustomerComplaintVO customercomplaintvo){
		Boolean returnStatus=false;
		CustomerComplaintServiceImpl customercomplaintServiceImpl = new CustomerComplaintServiceImpl();
		try {
			returnStatus= customercomplaintServiceImpl.createCustomerComplaint(customercomplaintvo);
		} catch (BadDaoCallException e) {
			e.printStackTrace();
		}
		return returnStatus;
	}
  
  public boolean updateCustomerComplaint(CustomerComplaintVO customercomplaintvo){ //Parameter CustomerComplaintVO
		CustomerComplaintServiceImpl customercomplaintServiceImpl = new CustomerComplaintServiceImpl();
		
	  	return customercomplaintServiceImpl.updateCustomerComplaint(customercomplaintvo.getAccountNo(), customercomplaintvo.getComplaintId(), customercomplaintvo.getStatus());
	  }
  
  
  public List<CustomerComplaintVO> getCustomerComplaintID(CustomerComplaintVO customercomplaintvo){
	  	List<CustomerComplaintVO> lst = new ArrayList<CustomerComplaintVO>();
	  	try{
	  		
	  		CustomerComplaintServiceImpl customercomplaintServiceImpl = new CustomerComplaintServiceImpl();
	  		lst	=	customercomplaintServiceImpl.viewAllCustomerComplaintsByAccId(customercomplaintvo.getAccountNo().toString());
	  	
	  	}catch(Exception e){
	  		e.printStackTrace();
	  	}
	  	return lst;
	  }
	  
	  public CustomerComplaintVO viewCustomerComplaintDetail(CustomerComplaintVO customercomplaintvo){
	  	CustomerComplaintServiceImpl CustomerComplaintserviceimpl = new CustomerComplaintServiceImpl();
	  	return CustomerComplaintserviceimpl.viewCustomerComplaintById(customercomplaintvo.getAccountNo(), customercomplaintvo.getComplaintId());
	  }



}
